<template>
  <div>
    <el-container>
      <el-main>
        <kendoForm
          :kendo-form-items="kendoFormItems"
          :kendo-form="kendoForm"
          :kendo-params-value="kendoParamsValue"
        />
      </el-main>
    </el-container>
  </div>
</template>

<script>
  import kendoForm from '@/Views/components/kendo-vue-component/kendoForm/index.vue'
  export default {
    name: 'PrjOperateCmpt',
    components: {
      kendoForm
    },
    data() {
      return {
        dialogName: this.$attrs.data.dialogName,
        kendoFormItems: [
          {
            ref: 'PRJ_CD',
            remoteData: true,
            cascade: true,
            name: 'relPpPrjNorjNo',
            label: '父项目名称',
            paramName: 'pPrjNo',
            optionLabel: '--请选择--',
            controlType: 'dropDownList',
            required: true,
            filter: 'contains',
            dataType: 'json',
            transportReadUrl: '/service/prjMng/prjTransDict',
            autoBind: true,
            schema: {
              data: function(response) {
                if (typeof response.data === 'object') {
                  if (JSON.stringify(response.data) === '{}') {
                    return []
                  }
                }
                return response.data
              }
            },
            dataMapping: {
              label: 'dictValNm',
              value: 'dictVal'
            },
            parameterMap: function(options, operation) {
              if (operation !== 'read' && options.models) {
                // eslint-disable-next-line no-undef
                return { models: kendo.stringify(options.models) }
              } else {
                // eslint-disable-next-line no-undef
                return kendo.stringify({ dict: 'PRJ_CD' })
              }
            }
          },
          {
            name: 'prjNo',
            label: '子项目编号',
            paramName: 'prjNo',
            required: true,
            controlType: 'input'
          },
          {
            name: 'prjName',
            label: '子项目名称',
            paramName: 'prjName',
            required: true,
            controlType: 'input'
          },
          {
            ref: 'PRJ_CLAF_CD',
            remoteData: true,
            cascade: true,
            name: 'prjClaf',
            label: '项目分类',
            paramName: 'prjClaf',
            optionLabel: '--请选择--',
            controlType: 'dropDownList',
            required: true,
            filter: 'contains',
            dataType: 'json',
            transportReadUrl: '/service/sysMng/dict/findDictByTypeWithForm',
            autoBind: true,
            schema: {
              data: function(response) {
                return response.data
              }
            },
            dataMapping: {
              label: 'dictValNm',
              value: 'dictVal'
            },
            parameterMap: function(options, operation) {
              if (operation !== 'read' && options.models) {
                // eslint-disable-next-line no-undef
                return { models: kendo.stringify(options.models) }
              } else {
                // eslint-disable-next-line no-undef
                return kendo.stringify({ dict: 'PRJ_CLAF_CD' })
              }
            }
          },
          {
            ref: 'PRJ_STATUS_CD',
            remoteData: true,
            cascade: true,
            name: 'prjStatus',
            label: '项目状态',
            paramName: 'prjStatus',
            optionLabel: '--请选择--',
            controlType: 'dropDownList',
            required: true,
            filter: 'contains',
            dataType: 'json',
            transportReadUrl: '/service/sysMng/dict/findDictByTypeWithForm',
            autoBind: true,
            schema: {
              data: function(response) {
                return response.data
              }
            },
            dataMapping: {
              label: 'dictValNm',
              value: 'dictVal'
            },
            parameterMap: function(options, operation) {
              if (operation !== 'read' && options.models) {
                // eslint-disable-next-line no-undef
                return { models: kendo.stringify(options.models) }
              } else {
                // eslint-disable-next-line no-undef
                return kendo.stringify({ dict: 'PRJ_STATUS_CD' })
              }
            }
          },
          {
            name: 'prjPeriodSt',
            label: '项目开始时间',
            paramName: 'prjPeriodSt',
            controlType: 'datePicker',
            format: 'yyyy-MM-dd',
            change: this.change,
            required: true
          },
          {
            name: 'prjPeriodEt',
            label: '项目结束时间',
            paramName: 'prjPeriodEt',
            controlType: 'datePicker',
            format: 'yyyy-MM-dd',
            required: true
          },
          {
            genre: 'special',
            label: '项目描述',
            name: 'prjDesc',
            paramName: 'prjDesc',
            controlType: 'textarea'
          }
        ],
        kendoForm: {
          ref: 'prodForm',
          valid: 'valid',
          buttons: [
            {
              name: '保存', // 按钮内容
              class: 'k-primary', // 按钮样式
              icon: 'save',
              click: this.save
            },
            {
              name: '重置', // 按钮内容
              icon: 'reset-sm',
              click: this.reset
            }
          ]
        },
        kendoParamsValue: {
          id: '',
          pPrjNo: '',
          prjNo: '',
          prjName: '',
          prjClaf: '',
          prjStatus: '',
          prjPeriodSt: '',
          prjPeriodEt: '',
          prjDesc: ''
        }
      }
    },
    mounted() {
      const operatorType = this.$attrs.data.operatorType
      if (operatorType === 'edit') {
        this.kendoParamsValue = Object.assign(this.kendoParamsValue, this.deepCopy(this.$attrs.data.param))
      }
      if (operatorType === 'add') {
        this.kendoParamsValue.pPrjNo = this.$attrs.data.param.prjNo
      }
    },
    methods: {
      save(paramVal, validator) {
        if (validator.validate()) {
          this.$post('/service/prjMng/savePrjInfo', paramVal).then(res => {
            if (res.code !== 200) {
              this.$KendoNotify({
                title: '错误信息:' + res.code,
                message: res.message
              }, 'error')
            } else {
              this.$KendoNotify({
                title: '提示信息:',
                message: res.message
              }, 'success')
              this.$bus.emit('dialog' + `${this.dialogName}`, true)
            }
          })
        }
      },
      reset(paramVal) {
        Object.keys(paramVal).forEach(key => {
          if (Array.isArray(paramVal[key])) {
            this.kendoParamsValue[key] = []
          } else {
            this.kendoParamsValue[key] = ''
          }
        })
      }
    }
  }
</script>

<style>

</style>
